package auctions;

import jade.core.AID;
import jade.core.Agent;

import jade.domain.FIPANames;

import jade.lang.acl.ACLMessage;

import java.util.Date;

import javax.swing.SwingUtilities;


public class SellerAgent extends Agent {

    public static final String EN = "en";
    public static final String DU = "du";
    //the price for object(s)
    private int price;
    //the list of buyers
    private AID[] buyers;
    //the number of objects
    private int numOfObjects;
    
    public static final char SEPARATOR = ',';
  
    protected static UserInterface gui;
    
    public static int getNumOfItemsFromMessage(String content) {
        return Integer.parseInt(content.substring(0, 
            content.indexOf(SEPARATOR)));
    }
  
    public static int getPriceFromMessage(String content) {
        return Integer.parseInt(content.substring(
            1 + content.indexOf(SEPARATOR)));  
    }
    
    public static String buildContent(int price, int numOfItems) {
        return "" + numOfItems + SEPARATOR + price;
    }
    
    protected void setup() {
        Object[] arguments = getArguments();
        if(arguments.length < 1) {
            System.out.println("Usage agentName [english price |" +
                "dutch price object] [buyers]+");
            doDelete();
        } else {
            //get the price for the item
            if(arguments[0].toString().equals(EN)) {
                //setup the gui
                setupGui(arguments.length, arguments, 2);
                try {
                    price = Integer.parseInt(arguments[1].toString());
                } catch (NumberFormatException e) {
                    price = 100;
                } catch (NullPointerException e) {
                    price = 100;      
                }
                //add all buyers
                addBuyers(arguments.length, 2, arguments);
                //add the behaviour for selling the item
                addEnglishSellerBehaviour();
            } else {
                //setup the gui
                setupGui(arguments.length, arguments, 3);
                try {
                    numOfObjects = Integer.parseInt(arguments[2].toString());
                    price = Integer.parseInt(arguments[1].toString());
                } catch (NumberFormatException e) {
                    numOfObjects = 3;
                    price = 500;
                } catch (NullPointerException e) {
                    numOfObjects = 3;
                    price = 500;
                }
                //add all buyers
                addBuyers(arguments.length, 3, arguments);
                //add the behaviour for selling the items
                addDutchSellerBehaviour();
            }
            addBehaviour(new InterfaceBehviour(this));
        }
    }

    private void addBuyers(int total, int start, Object[] arguments) {
        buyers = new AID[total - start];
        for (int i = start; i < total; i++) {
            buyers[i - start] = new AID(arguments[i].toString(), 
                                        AID.ISLOCALNAME);
        }
    }

    private void addEnglishSellerBehaviour() {
        ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);
        for (AID aID : buyers) {
            msg.addReceiver(aID);
        }
        msg.setProtocol(FIPANames.InteractionProtocol.FIPA_ENGLISH_AUCTION);
        msg.setReplyByDate(new Date(System.currentTimeMillis() + 10000));
        msg.setContent("" + price);
        addBehaviour(new EnglishSeller(this, msg));
    }

    private void addDutchSellerBehaviour() {
        ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);
        for (AID aID : buyers) {
            msg.addReceiver(aID);
        }
        msg.setProtocol(FIPANames.InteractionProtocol.FIPA_DUTCH_AUCTION);
        msg.setReplyByDate(new Date(System.currentTimeMillis() + 10000));
        msg.setContent(buildContent(price, numOfObjects));
        addBehaviour(new DutchSeller(this, msg));
    }
    
    private void setupGui(int numOfBuyers, Object[] args, int start) {
        String [] names = new String[numOfBuyers - start];
        for(int i = start;i < args.length;i++) {
            names[i - start] = args[i].toString();
        }
        
        gui = new UserInterface(numOfBuyers - start, names);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                gui.setVisible(true);
            }
        });
    }
    
    void addMessage(String content) {
        gui.addAuctionMessage(content);
    }
    
    void updateBuyer(String buyerName, String content) {
        gui.updateBuyerInfo(buyerName, getPriceFromMessage(content), 
                            getNumOfItemsFromMessage(content));
    }
}
